package com.woniuxy.service;

import com.woniuxy.entity.WeatherDTO;
import com.woniuxy.entity.WeatherResponse;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Service
@RequiredArgsConstructor
public class WeatherService {

    // d88931080c86456696a95950ededfe93
    private final String apiKey = "d88931080c86456696a95950ededfe93";
    private final RestTemplate restTemplate;  // 依赖注入

    public WeatherDTO getDateWeather(String date) {
        String url = "https://devapi.qweather.com/v7/weather/7d?location=101270101&key=" + apiKey;
        WeatherResponse response = restTemplate.getForObject(url, WeatherResponse.class);

        if (response != null && "200".equals(response.getCode())) {
            return response.getDaily().stream()
                    .filter(d -> date.equals(d.getFxDate()))
                    .findFirst()
                    .orElse(null);
        }
        return null;
    }
}